﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;
namespace DAO
{
    public class BanHangDAO
    {
        public void ThemDonDatHang(BanHangDTO bhDTO)
        {
            SqlConnection cn = DataProvider.ConnectionData();
            string sql = "sp_ThemDonDatHang";
            SqlCommand cmd = new SqlCommand(sql, cn);
            cmd.CommandType = CommandType.StoredProcedure;
      
            cmd.Parameters.Add("@maKH", bhDTO.MaKH);
            cmd.Parameters.Add("@nguoiNhan ", bhDTO.NguoiNhan);
            cmd.Parameters.Add("@sdtNguoiNhan ",bhDTO.SDTNguoiNhan );
            cmd.Parameters.Add("@diaChiGiaoHang ",bhDTO.DiaChiGiaoHang );
            cmd.Parameters.Add("@thoiGianDatHang",bhDTO.ThoiGianDatHang.ToShortDateString());
            cmd.Parameters.Add("@thoiGianGiaoHang ", bhDTO.ThoiGianGiaohang.ToShortDateString());
            cmd.Parameters.Add("@tenTrangTri ", bhDTO.TenTrangTri);
            cmd.Parameters.Add("@ghiChu",bhDTO.GhiChu );
            cmd.Parameters.Add("@vAT ",bhDTO.VAT );
            cmd.Parameters.Add("@phuThu ", bhDTO.PhuThu);
            cmd.Parameters.Add("@tongTien ",bhDTO.TongTien );
            cmd.Parameters.Add("@tinhTrangThanhToan", bhDTO.TinhTrang);
            cmd.Parameters.Add("@tinhTrang", bhDTO.TinhTrang);
            
            cmd.ExecuteNonQuery();
            cn.Close();
        }
        public void ThemChiTietHoaDon(ChiTietHoaDonDTO cthdDTO)
        {
            SqlConnection cn = DataProvider.ConnectionData();
            string sql = "sp_ThemChiTietHoaDon";
            SqlCommand cmd = new SqlCommand(sql, cn);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add("@maDH ", cthdDTO.MaDH);
            cmd.Parameters.Add("@maHoa", cthdDTO.MaHoa);
            cmd.Parameters.Add("@soLuong ", cthdDTO.SoLuong);
            cmd.Parameters.Add("@giaHoa ", cthdDTO.GiaHoa);
           
            cmd.ExecuteNonQuery();
            cn.Close();
        }
        public int LayMaDatHang()
        {
            SqlConnection cn = DataProvider.ConnectionData();
            string sql = "sp_LayMaDatHang";
            SqlCommand cmd = new SqlCommand(sql, cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@maDH", SqlDbType.Int);
            cmd.Parameters["@maDH"].Direction = ParameterDirection.Output;
            cmd.Parameters["@maDH"].Value = 0;
            cmd.ExecuteNonQuery();
            int maDH = Int32.Parse(cmd.Parameters["@maDH"].Value.ToString());
            cn.Close();
            return maDH;
        }
        public int CapNhatSoLuongHoa(int maHoa, int soLuongBan)
        {
            SqlConnection cn = DataProvider.ConnectionData();
            string sql = "sp_CapNhatSoLuongHoa";
            SqlCommand cmd = new SqlCommand(sql, cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@maHoa", maHoa);
            cmd.Parameters.Add("@soLuongDaBan", soLuongBan);
            cmd.Parameters.Add("@kq", SqlDbType.Int);
            cmd.Parameters["@kq"].Direction = ParameterDirection.Output;
            cmd.Parameters["@kq"].Value = 0;
            cmd.ExecuteNonQuery();
            int kq = Int32.Parse(cmd.Parameters["@kq"].Value.ToString());
            cn.Close();
            return kq;
        }
        public static DataTable TimKKiemHoaDon(string ngayDat, string tenKhachHang)
        {
            DataTable dt = new DataTable();
            SqlConnection cn = DataProvider.ConnectionData();
            string sql = "sp_TimKiemHoaDon";
            SqlCommand cmd = new SqlCommand(sql, cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@ngayDat", ngayDat);
            cmd.Parameters.Add("@tenKhachHang", tenKhachHang);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            cn.Close();
            return dt;
        }
    }
}
